<template>
  <div class="container">
    <div class="app-container" style="padding: 20px;">
      <el-row class="header" style="font-size: 14px;">
        <el-input
          v-model="searchForm.companyId"
          placeholder="请输入企业名称"
          size="small"
          style="width: 200px; margin-right: 10px;"
        />
        <el-date-picker
          v-model="searchForm.dateRange"
          type="daterange"
          align="right"
          size="small"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="margin-right: 10px;"
        />
        <el-button size="small" type="primary" @click="searchBills">查询</el-button>
        <el-button size="small" type="success" @click="showAddBillDialog">添加账单</el-button>
      </el-row>
      <el-divider />
      <el-table :data="billsList" style="width: 100%; margin-top: 20px;">
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="billNumber" label="账单编号" />
        <el-table-column prop="companyName" label="企业名称" />
        <el-table-column prop="building" label="租赁楼宇" />
        <el-table-column prop="propertyFee" label="物业费(元/m²)" />
        <el-table-column prop="amount" label="账单金额(元)" />
        <el-table-column prop="paymentTime" label="缴费时间" />
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="mini" @click="viewBill(scope.row)">查看</el-button>
            <el-button size="mini" type="danger" @click="deleteBill(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row style="margin-top: 20px;" type="flex" justify="end">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, prev, pager, next, sizes, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>

    <!-- 添加/编辑账单对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
      <el-form :model="form" label-width="120px">
        <el-form-item label="账单编号">
          <el-input v-model="form.billNumber" disabled />
        </el-form-item>
        <el-form-item label="企业名称">
          <el-input v-model="form.companyName" />
        </el-form-item>
        <el-form-item label="租赁楼宇">
          <el-input v-model="form.building" />
        </el-form-item>
        <el-form-item label="物业费(元/m²)">
          <el-input v-model="form.propertyFee" type="number" />
        </el-form-item>
        <el-form-item label="账单金额(元)">
          <el-input v-model="form.amount" type="number" />
        </el-form-item>
        <el-form-item label="缴费时间">
          <el-date-picker v-model="form.paymentTime" type="date" placeholder="选择日期" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveBill">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchForm: {
        companyId: '',
        dateRange: []
      },
      billsList: [],
      totalBills: 0,
      pageSize: 10,
      currentPage: 1,
      dialogVisible: false,
      dialogTitle: '添加账单',
      form: {
        id: '',
        billNumber: '',
        companyName: '',
        building: '',
        propertyFee: '',
        amount: '',
        paymentTime: ''
      }
    }
  },
  created() {
    this.fetchBills()
  },
  methods: {
    showAddBillDialog() {
      this.dialogTitle = '添加账单'
      this.resetForm()
      this.dialogVisible = true
    },
    viewBill(bill) {
      console.log('查看账单:', bill)
    },
    deleteBill(id) {
      this.$confirm('确定要删除这条账单吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用删除账单的 API
        console.log('删除账单:', id)
      })
    },
    saveBill() {
      // 调用保存账单的 API
      console.log('保存账单:', this.form)
      this.dialogVisible = false
    },
    resetForm() {
      this.form = {
        id: '',
        billNumber: '',
        companyName: '',
        building: '',
        propertyFee: '',
        amount: '',
        paymentTime: ''
      }
    },
    handlePageChange(page) {
      this.currentPage = page
      this.fetchBills()
    },
    searchBills() {
      // 调用搜索账单的 API
      console.log('搜索账单:', this.searchForm)
      this.fetchBills()
    },
    fetchBills() {
      // 调用获取账单列表的 API
      console.log('获取账单列表:', this.currentPage)
    }
  }
}
</script>

<style scoped>
.container {
  padding: 20px;
}

.dialog-footer {
  text-align: right;
}

.header {
  margin-bottom: 20px;
}
</style>
